﻿using System.Net;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Principal;
using System.Web.Routing;
using System.Web.Hosting;
namespace System.Web
{
    public class NetHttpRequest : HttpRequestWrapper
    {
        private HttpListenerContext _netContext;
        private HttpListenerRequest _netRequest;
        private HttpBrowserCapabilitiesBase _browserCapabilities;
        private HttpFileCollectionBase _files;

        public NetHttpRequest(WebHostWorkerRequest wr, NetHttpContext context)
            : base(new HttpRequest(null, "http://contoso", null))
        {
            Context = context;
            _netContext = wr.NetContext;
            _netRequest = _netContext.Request;
        }
        internal void Dispose()
        {
        }

        internal NetHttpContext Context { get; set; }

        #region HttpRequestBase

        public override byte[] BinaryRead(int count)
        {
            var b = new byte[count];
            int readCount = _netRequest.InputStream.Read(b, 0, count);
            if (readCount == count)
                return b;
            var b2 = new byte[readCount];
            Array.Copy(b, b2, readCount);
            return b2;
        }

        public override int[] MapImageCoordinates(string imageFieldName) { return null; }
        public override string MapPath(string virtualPath) { return null; }
        public override string MapPath(string virtualPath, string baseVirtualDir, bool allowCrossAppMapping) { return null; }
        public override void SaveAs(string filename, bool includeHeaders) { }
        public override void ValidateInput() { }

        public override string[] AcceptTypes
        {
            get { return null; }
        }

        public override string AnonymousID
        {
            get { return null; }
        }

        public override string ApplicationPath
        {
            get { return "/"; }
        }

        public override string AppRelativeCurrentExecutionFilePath
        {
            get { return "~" + _netRequest.Url.LocalPath; }
        }

        public override HttpBrowserCapabilitiesBase Browser
        {
            get { return _browserCapabilities; }
        }

        public override HttpClientCertificate ClientCertificate
        {
            get { return null; }
        }

        public override Encoding ContentEncoding
        {
            get { return _netRequest.ContentEncoding; }
            set { }
        }

        public override int ContentLength
        {
            get { return (int)_netRequest.ContentLength64; }
        }

        public override string ContentType
        {
            get { return _netRequest.ContentType; }
            set { }
        }

        public override HttpCookieCollection Cookies
        {
            get
            {
                var cookies = new HttpCookieCollection();
                foreach (Cookie cookie in _netRequest.Cookies)
                    cookies.Add(new HttpCookie(cookie.Name, cookie.Value)
                    {
                        Expires = cookie.Expires,
                    });
                return cookies;
            }
        }

        public override string CurrentExecutionFilePath
        {
            get { return "/"; }
        }

        public override string FilePath
        {
            get { return "/"; }
        }

        public override HttpFileCollectionBase Files
        {
            get { return _files; }
        }

        public override Stream Filter
        {
            get { return null; }
            set { }
        }

        public override NameValueCollection Form
        {
            get { return null; }
        }

        public override NameValueCollection Headers
        {
            get { return _netRequest.Headers; }
        }

        public override ChannelBinding HttpChannelBinding
        {
            get { return null; }
        }

        public override string HttpMethod
        {
            get { return _netRequest.HttpMethod; }
        }

        public override Stream InputStream
        {
            get { return _netRequest.InputStream; }
        }

        public override bool IsAuthenticated
        {
            get { return _netRequest.IsAuthenticated; }
        }

        public override bool IsLocal
        {
            get { return _netRequest.IsLocal; }
        }

        public override bool IsSecureConnection
        {
            get { return _netRequest.IsSecureConnection; }
        }

        public override string this[string key]
        {
            get { return null; }
        }

        public override WindowsIdentity LogonUserIdentity
        {
            get { return null; }
        }

        public override NameValueCollection Params
        {
            get { return null; }
        }

        public override string Path
        {
            get { return _netRequest.Url.PathAndQuery; }
        }

        public override string PathInfo
        {
            get { return string.Empty; }
        }

        public override string PhysicalApplicationPath
        {
            get { return "/"; }
        }

        public override string PhysicalPath
        {
            get { return "/"; }
        }

        public override NameValueCollection QueryString
        {
            get { return _netRequest.QueryString; }
        }

        public override string RawUrl
        {
            get { return _netRequest.RawUrl; }
        }

        public override RequestContext RequestContext
        {
            get { return base.RequestContext; }
        }

        public override string RequestType
        {
            get { return null; }
            set { }
        }

        public override NameValueCollection ServerVariables
        {
            get { return null; }
        }

        public override int TotalBytes
        {
            get { return 0; }
        }

        public override Uri Url
        {
            get { return _netRequest.Url; }
        }

        public override Uri UrlReferrer
        {
            get { return _netRequest.UrlReferrer; }
        }

        public override string UserAgent
        {
            get { return _netRequest.UserAgent; }
        }

        public override string UserHostAddress
        {
            get { return _netRequest.UserHostAddress; }
        }

        public override string UserHostName
        {
            get { return _netRequest.UserHostName; }
        }

        public override string[] UserLanguages
        {
            get { return _netRequest.UserLanguages; }
        }

        #endregion

        public bool IsDebuggingRequest { get; set; }
    }
}
